from rest_framework.generics import ListAPIView
from rest_framework.viewsets import ModelViewSet

from goods.models import SPU, Brand, GoodsCategory
from sadmin.serializersss.spu_serializers import SPUserializers, BrandSimpleSerializers, ChannelCategorySerializers
from sadmin.utils import Mypage


class SPUGoodsView(ModelViewSet):
    queryset = SPU.objects.all()
    serializer_class = SPUserializers
    pagination_class = Mypage
    def get_queryset(self):
        data = self.request.query_params.get("keyword")
        # kp = self.kwargs.get('pk')
        if data:
            query = SPU.objects.filter(name__contains=data)
            return query
        # if kp:
        #     return SPU.objects.filter(pk=kp)
        return SPU.objects.all()


class BrandSimpleView(ListAPIView):
    queryset = Brand.objects.all()
    serializer_class = BrandSimpleSerializers


class ChannelCategoryView(ModelViewSet):
    queryset = GoodsCategory.objects.all()
    serializer_class = ChannelCategorySerializers
    def get_queryset(self):
        pk = self.kwargs.get('pk')
        if pk:
            querysets = GoodsCategory.objects.filter(parent_id=pk)
            return querysets
        return GoodsCategory.objects.filter(parent_id=None)